﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="Pages_Operators_ProviderList" Codebehind="ProviderList.aspx.cs" %>

<%@ Register Src="../Operators/Ascx/ProviderContactPopup.ascx" TagName="ProviderContactPopup"
    TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="Server">
    Provider
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="using" runat="Server">

    <script type="text/javascript" src="/Pages/Operators/Js/provider.js"></script>

    <script type="text/javascript" src="/js/smartpaginator.js"></script>

    <link href="/css/smartpaginator.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="content" runat="Server">
    <div style="margin: 5px">
        <table id="condition">
            <tr>
                <td>
                    <!-- Title -->
                    <h1>
                        Search Providers</h1>
                </td>
            </tr>
            <tr>
                <!-- Provider #, Provider Name, Email -->
                <td>
                    <div class="label">
                        Provider#:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtProviderNumber" runat="server"></asp:TextBox>
                    </div>
                </td>
                <td>
                    <div class="label">
                        Provider Name:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtProviderName" runat="server"></asp:TextBox>
                    </div>
                </td>
                <td>
                    <div class="label">
                        Email:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    </div>
                </td>
            </tr>
            <tr>
                <!-- Website, Address, City -->
                <td>
                    <div class="label">
                        Website:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtWebsite" runat="server"></asp:TextBox>
                    </div>
                </td>
                <td>
                    <div class="label">
                        Address:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtAddressLine" runat="server"></asp:TextBox>
                    </div>
                </td>
                <td>
                    <div class="label">
                        City:</div>
                    <div class="field">
                        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    </div>
                </td>
            </tr>
            <tr>
                <%-- Phone, Fax, Find and Excel --%>
                <td>
                    <div class="label">
                        Phone:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
                    </div>
                </td>
                <%-- Provider Type ID --%>
                <td>
                    <div class="label">
                        Fax:
                    </div>
                    <div class="field">
                        <asp:TextBox ID="txtFaxNumber" runat="server"></asp:TextBox>
                    </div>
                </td>
                <%-- Button Find --%>
                <td colspan="2">
                    <a id="btnFind" class="button" href="javascript:void(0)"><span>Search</span></a>
                    <a id="btnExcel" runat="server" class="button" href="javascript:void(0)"><span>Excel</span></a>
                </td>
            </tr>
        </table>
        <div id="provider">
        </div>
        <div id="red" style="margin: auto;">
        </div>
    </div>
    <%-- Results Table Template --%>

    <script type="text/javascript">
        // Variables
        var pageSize = 20;

        $(document).ready(function () {

            // Data Methods
            function getData(pageNumber, pagelink) {
                loading($('#provider'));

                var providerNumber = $("#<%=txtProviderNumber.ClientID %>").val();
                var providerName = $("#<%=txtProviderName.ClientID %>").val();
                var email = $("#<%=txtEmail.ClientID %>").val();
                var website = $("#<%=txtWebsite.ClientID %>").val();
                var addressLine = $("#<%=txtAddressLine.ClientID %>").val();
                var city = $("#<%=txtCity.ClientID %>").val();
                var phone = $("#<%=txtPhone.ClientID %>").val();
                var faxNumber = $("#<%=txtFaxNumber.ClientID %>").val();

                parameters = "{providerNumber:'" + providerNumber +
                "', providerName: '" + providerName +
                "', email:'" + email +
                "', website:'" + website +
                "', addressLine:'" + addressLine +
                "', city:'" + city +
                "', phone:'" + phone +
                "', faxNumber:'" + faxNumber +
                "', pageSize:" + pageSize +
                ", pageNumber:" + pageNumber + "}";

                $.ajax({
                    type: "POST",
                    url: "/Get.asmx/GetProviderList",
                    data: parameters,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: (function Success(data, status) {
                        var list = data.d;
                        $('#provider').setTemplateURL("/Pages/Operators/template/providerListTemplate.htm");
                        $('#provider').processTemplate(list);

                        // setPageNumber();

                        if (pagelink != null) {
                            $('#red').smartpaginator({
                                totalrecords: list.TotalRecords,
                                recordsperpage: list.PageSize,
                                length: 8,
                                next: 'Next',
                                prev: 'Prev',
                                first: 'First',
                                last: 'Last',
                                theme: 'red',
                                controlsalways: true,
                                onchange: function (newPage) {
                                    getData(newPage);
                                    $('#r').html('Page # ' + newPage);
                                }
                            });
                        }

                        defineButtons();
                    }),
                    error: (function Error(request, status, error) {
                        $("#provider").html(request.statusText).fadeIn(1000);
                    })
                });
            }

            function defineButtons() {
                $("#gridProvider a.btnNew").button();

                $("#gridProvider a.btnDelete").click(function () {

                    var result = confirm("Are you sure you want to delete this record?");
                    if (result) {
                        var aTag = $(this);

                        // load provider
                        $.ajax({
                            type: "POST",
                            url: "/Update.asmx/DeleteProvider",
                            data: "{ providerID:'" + aTag.attr("providerid") + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            async: false,
                            success: (function Success(data, status) {
                                if (data.d) {
                                    // Select provider
                                    $('#gridProvider > tbody > tr').removeClass('selected');
                                    aTag.parents("tr:first").remove();
                                    return false;
                                }
                                else {

                                }
                            }),
                            error: (function Error(request, status, error) {
                                $("#provider").html(request.statusText).fadeIn(1000);
                            })
                        });
                    }
                });
            }

            $("#btnFind").click(function () {
                getData(1, true);
            });

            $("#btnExcel").click(function () {
                getData(1, true);
            });

            $(".field").keydown(function (event) {
                if (event.keyCode == '13') {
                    getData(1, true);
                    return false;
                }
            });

            getData(1, true);
        });
        
    </script>

</asp:Content>
